// CyberPunkCafeApp.java
import java.util.List;
import java.util.Scanner;

public class CyberPunkCafeApp {
    private static CoffeeShop coffeeShop;
    private static Scanner scanner;
    
    public static void main(String[] args) {
        scanner = new Scanner(System.in);
        coffeeShop = new CoffeeShop();
        
        // 加载保存的数据
        FileManager.loadCustomersFromFile(coffeeShop.getCustomersMap(), "顾客数据.txt");
        FileManager.loadOrdersFromFile(coffeeShop.getOrders(), "订单数据.txt");
        
        boolean running = true;
        while (running) {
            System.out.println("\n===== 东方红咖啡屋管理系统 =====");
            System.out.println("1. 点餐");
            System.out.println("2. 查看菜单");
            System.out.println("3. 查看订单历史");
            System.out.println("4. 顾客管理");
            System.out.println("5. 退出并保存");
            System.out.print("请选择操作: ");
            
            int choice = getIntInput(1, 5);
            
            switch (choice) {
                case 1:
                    placeOrder();
                    break;
                case 2:
                    displayMenu();
                    break;
                case 3:
                    displayOrderHistory();
                    break;
                case 4:
                    manageCustomers();
                    break;
                case 5:
                    running = false;
                    break;
            }
        }
        
        // 保存数据
        FileManager.saveCustomersToFile(coffeeShop.getCustomersMap(), "顾客数据.txt");
        FileManager.saveOrdersToFile(coffeeShop.getOrders(), "订单数据.txt");
        
        System.out.println("数据已保存，谢谢使用！");
        scanner.close();
    }
    
    private static void placeOrder() {
        System.out.println("\n----- 点餐 -----");
        
        // 获取或创建顾客
        System.out.print("请输入顾客姓名: ");
        String name = scanner.nextLine();
        
        Customer customer = coffeeShop.findCustomer(name);
        if (customer == null) {
            System.out.print("新顾客！请输入初始余额: ");
            double balance = getDoubleInput();
            customer = new Customer(name, balance);
            coffeeShop.addCustomer(customer);
        }
        
        // 显示菜单
        displayMenu();
        
        // 选择咖啡
        List<Coffee> menu = coffeeShop.getMenu();
        System.out.print("请选择咖啡编号: ");
        int coffeeIndex = getIntInput(1, menu.size()) - 1;
        Coffee selectedCoffee = menu.get(coffeeIndex);
        
        // 选择数量
        System.out.print("请输入数量: ");
        int quantity = getIntInput(1, 10);
        
        // 创建订单
        Order order = new Order(customer, selectedCoffee, quantity);
        
        // 选择支付方式
        List<Payment> payments = coffeeShop.getPaymentMethods();
        System.out.println("\n请选择支付方式:");
        for (int i = 0; i < payments.size(); i++) {
            System.out.println((i+1) + ". " + payments.get(i).getPaymentMethod());
        }
        
        int paymentIndex = getIntInput(1, payments.size()) - 1;
        Payment selectedPayment = payments.get(paymentIndex);
        
        // 处理订单
        if (order.processOrder(selectedPayment)) {
            coffeeShop.addOrder(order);
            System.out.println("订单成功！");
            order.displayOrder();
        } else {
            System.out.println("订单失败，请重试或选择其他支付方式");
        }
    }
    
    private static void displayMenu() {
        System.out.println("\n----- 菜单 -----");
        List<Coffee> menu = coffeeShop.getMenu();
        for (int i = 0; i < menu.size(); i++) {
            System.out.print((i+1) + ". ");
            menu.get(i).displayInfo();
        }
    }
    
    private static void displayOrderHistory() {
        System.out.println("\n----- 订单历史 -----");
        List<Order> orders = coffeeShop.getOrderHistory();
        
        if (orders.isEmpty()) {
            System.out.println("暂无订单历史");
            return;
        }
        
        for (Order order : orders) {
            order.displayOrder();
            System.out.println("----------------------");
        }
    }
    
    private static void manageCustomers() {
        System.out.println("\n----- 顾客管理 -----");
        // 实现顾客管理功能
        System.out.println("1. 查看所有顾客");
        System.out.println("2. 查看顾客详情");
        System.out.print("请选择: ");
        
        int choice = getIntInput(1, 2);
        
        if (choice == 1) {
            System.out.println("所有顾客:");
            for (String name : coffeeShop.getCustomersMap().keySet()) {
                System.out.println("- " + name);
            }
        } else {
            System.out.print("请输入顾客姓名: ");
            String name = scanner.nextLine();
            Customer customer = coffeeShop.findCustomer(name);
            
            if (customer != null) {
                System.out.println("顾客: " + customer.getName());
                System.out.println("能量值: " + customer.getEnergyLevel());
                System.out.println("余额: ¥" + customer.getBalance());
                
                // 显示该顾客的订单
                List<Order> customerOrders = coffeeShop.getOrdersByCustomer(name);
                System.out.println("订单数量: " + customerOrders.size());
            } else {
                System.out.println("未找到顾客: " + name);
            }
        }
    }
    
    // 辅助方法：获取整数输入
    private static int getIntInput(int min, int max) {
        while (true) {
            try {
                int input = Integer.parseInt(scanner.nextLine());
                if (input >= min && input <= max) {
                    return input;
                } else {
                    System.out.print("请输入" + min + "到" + max + "之间的数字: ");
                }
            } catch (NumberFormatException e) {
                System.out.print("请输入有效数字: ");
            }
        }
    }
    
    // 辅助方法：获取浮点数输入
    private static double getDoubleInput() {
        while (true) {
            try {
                return Double.parseDouble(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.print("请输入有效数字: ");
            }
        }
    }
}
